package cn.livingCloud.service.impl;

import cn.livingCloud.dto.CaseCategoryDto;
import cn.livingCloud.dto.CaseCategoryFormDto;
import cn.livingCloud.dto.CaseCategoryListDto;
import cn.livingCloud.entity.core.CaseCategory;
import cn.livingCloud.repository.CaseCategoryMapper;
import cn.livingCloud.service.CaseCategoryService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by 唐旭 on 2016/6/2.
 */
@Service
public class CaseCategoryServiceImpl implements CaseCategoryService {

    @Autowired
    private CaseCategoryMapper mapper;


    @Override
    public void loadList(CaseCategoryListDto listDto) {
        String parentUuid = listDto.getParentUuid();
        if (StringUtils.isNotEmpty(parentUuid)) {
            CaseCategory category = mapper.selectByPrimaryKey(parentUuid);
            listDto.setParentCategoryDto(new CaseCategoryDto(category));
        }
        listDto.addAllList(mapper.getList(listDto));
    }

    @Override
    public CaseCategoryFormDto loadForm(String uuid, String parentUuid) {
        CaseCategoryFormDto formDto;
        if (StringUtils.isNotEmpty(uuid)) {
            CaseCategory caseCategory = mapper.selectByPrimaryKey(uuid);
            formDto = new CaseCategoryFormDto(caseCategory);
        } else {
            formDto = new CaseCategoryFormDto();
            if (StringUtils.isNotEmpty(parentUuid)) {
                formDto.setParent(new CaseCategoryDto(mapper.selectByPrimaryKey(parentUuid)));
            }
        }
        return formDto;
    }

    @Override
    public void update(CaseCategoryFormDto formDto) {
        String uuid = formDto.getUuid();
        if (StringUtils.isNotEmpty(uuid)) {
            CaseCategory caseCategory = mapper.selectByPrimaryKey(uuid);
            formDto.update(caseCategory);
            mapper.updateByPrimaryKeyWithBLOBs(caseCategory);
        } else {
            CaseCategory category = new CaseCategory();
            formDto.update(category);
            mapper.insert(category);
        }
    }

    @Override
    public String loadContent(String uuid) {
        CaseCategory category = mapper.selectByPrimaryKey(uuid);
        return category.getContent();
    }

    @Override
    public void delete(String uuid) {
        CaseCategory category = mapper.selectByPrimaryKey(uuid);
        category.delete();
    }
}
